﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: DictionaryEquals.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System.Collections.Generic;

namespace BOL.Linq
{
    public static partial class Linq
    {
        public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
        {
            if (first == second)
                return true;

            if ((first == null) || (second == null))
                return false;
            
            if (first.Count != second.Count)
                return false;

            var comparer = EqualityComparer<TValue>.Default;

            foreach (var kvp in first)
            {
                TValue secondValue;

                if (!second.TryGetValue(kvp.Key, out secondValue))
                    return false;
                
                if (!comparer.Equals(kvp.Value, secondValue))
                    return false;
            }

            return true;
        } 

    }
}
